const Koa = require("koa");
const mongoose = require("mongoose");
const koaStatic = require("koa-static");
const koaBody = require("koa-body");
const error = require("koa-json-error");
const routingMap = require("./routes");
const koaParams = require("koa-parameter");
const path = require("path");
const answer = require("./answer.js");
const question = require("./question.js");
// const { MONGO_URL } = require("./config");

require("dotenv").config();

let options = {
  useNewUrlParser: true,
  useFindAndModify: false,
  useCreateIndex: true,
  autoIndex: false
};

answer.pages[0].questions.forEach((item) => {
  item.id
})

mongoose.connect(process.env.MONGO_URL, options);

mongoose.connection
  .on("open", () => {
    console.log("MongoDB Connected!");
  })
  .on("error", console.error);
const app = new Koa();

app.use(koaStatic(path.join(__dirname, "public")));
app.use(
  error({
    postFormat: (err, { stack, ...rest }) =>
      process.env.NODE_ENV === "production" ? rest : { stack, ...rest }
  })
);
app.use(
  koaBody({
    multipart: true,
    formidable: {
      uploadDir: path.join(__dirname, "/public/uploads"),
      keepExtensions: true
    }
  })
);
app.use(koaParams(app));
routingMap(app);

app.listen(3000, () => {
  console.log("server up on port 3000!");
});
